﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CardGamesLibrary.Cards;

namespace CardGamesLibrary.UI
{
    public partial class FrenchDeckPileControl : UserControl
    {
        ICardPile<FrenchDeckCard> _pile;

        public ICardPile<FrenchDeckCard> Pile
        {
            get { return _pile; }
            set { _pile = value; }
        }

        public FrenchDeckPileControl()
        {
            InitializeComponent();
        }
        public FrenchDeckPileControl(ICardPile<FrenchDeckCard> pile) : this()
        {
            _pile = pile;
            AllowDrop = true;
            _pile.PropertyChanged += new PropertyChangedEventHandler(_pile_PropertyChanged);
        }

        void _pile_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.Controls.Clear();
            if (!LastCardOnly)
            {
                int i = 0;
                var tablePanel = new TableLayoutPanel() { Dock = DockStyle.Fill };
                tablePanel.RowCount = _pile.Count+1;
                tablePanel.ColumnCount = 1;
                tablePanel.Padding = new Padding(0);
               
                foreach (var item in _pile)
                { 
                    i++;
                    var card = new FrenchDeckCardControl(item) { Dock = DockStyle.Fill};
                    if (i < _pile.Count)
                    {
                        card.Height = 30;
                    }
                    else {
                        card.Height = 200;
                    }
                    card.Click += new EventHandler(card_Click);
                    tablePanel.Controls.Add(card);
                    
                   
                }
                Controls.Add(tablePanel);
            }
            else {
                var card = new FrenchDeckCardControl(_pile.Last());
                card.Click += new EventHandler(card_Click);
                this.Controls.Add(card);
            }
        }

        void card_Click(object sender, EventArgs e)
        {
            OnClick(e);
        }

        public bool LastCardOnly { get; set; }

        protected override void OnPaint(PaintEventArgs e)
        {
           
            base.OnPaint(e);
        }

        private void FrenchDeckPileControl_Load(object sender, EventArgs e)
        {

        }

       
        protected override void OnDragEnter(DragEventArgs drgevent)
        {
            
            base.OnDragEnter(drgevent);
            if (drgevent.Data.GetData(typeof(FrenchDeckCard)) != null)
            {
                var frenchDeckCard = drgevent.Data.GetData(typeof(FrenchDeckCard)) as FrenchDeckCard;
                if (_pile.CanAdd(frenchDeckCard))
                {
                    drgevent.Effect = DragDropEffects.Move;
                    
                }
            } else {
                drgevent.Effect = DragDropEffects.None;
            }
            
        }

        protected override void OnDragDrop(DragEventArgs drgevent)
        {

            if (drgevent.Data.GetData(typeof(FrenchDeckCard)) != null)
            {
                var frenchDeckCard = drgevent.Data.GetData(typeof(FrenchDeckCard)) as FrenchDeckCard;
                if (_pile.CanAdd(frenchDeckCard))
                {
                    _pile.Add(frenchDeckCard);
                    Refresh();
                }
            }
            else
            {
                drgevent.Effect = DragDropEffects.None;
            }
            base.OnDragDrop(drgevent);
        }

        

    }
}
